Might come to the solution for coding exercise 16 and we import pandas.
PI and Matt prop lip and then we import our five U.S. stocks and the S&P 500 Total Return and we have
to select the appropriate price data to calculate total returns.
And uh we selected the adjusted to close prices because they include the price returns and dividends
and we have actually then to that aframe stocks and S&P 500 and then we select for both data frames
the five year period from 240 14 to 18 and we actually overwrite both data frames
so here we have our six constituents from uh the end of 213 to to 18 and the same we have for the S&P
500 Total Return Index and then we can also add to the S&P 500 Total Return Index here to our stocks
data frame by creating here an additional column and we can simply do this here by creating a new column
a market portfolio and assign here the column in the S&P 500 data frame at just that close and actually
the values here get automatically aligned to the index in our stocks data frame.
So this is straightforward actually
so here we are on the right hand side also the market portfolio and then we can create the daily simple
returns with the percentage change method and we create the data frame rate and uh then we can also
create the summary data frame with annualized the risk and return.
And also here we use uh the user defined function annualized risk and return so here we have the summary
data frame with our six constituents and the market portfolio and then we can also visualize so that
we're in a total risk return framework.
So yes the market portfolio and then the next step to calculate the sharp ratio we need the risk free
return and we assume that it is the one point seven percent and then we can calculate the sharp razor
which is the return minus the risk free return divided by the risk and uh we create the additional columns
harp
so here we have sharp and actually we have fear the annualized return then the annualized risk and standard
deviation units and the sharp ratio and we can also convert the risk in standard deviation units to
variance units by simply taking it to the power of two.
So we create the additional column total risk and variance units and we use the N put out the power
method.
So now here we have the total risk and variance units and then we can also create the annualized covariance
matrix and we call you the code method on the daily returns data frame and annualize that the covariance
by multiplying it with two hundred fifty two.
So that's here.
The covariance matrix and he on the right hand side we can actually see the core variances of the constituents
of the market portfolio.
And that's actually the systematic risk of uh the constituents.
And therefore we can also add the column systematic risk and variance units by taking here the core
variances with the market portfolios and then having the total risk and variance units and uh the systematic
risk then we can also calculate the and systematic risk which is simply the total risk minus the systematic
risk.
And also here we create an additional column for our summary data frame and let's have a look so here
we have a total risk.
Systematic risk and and systematic risk.
And then we can also visualize systematic risk and and systematic risk of each stock and the market
portfolio with a pop plot.
And the question is here which company shows most likely the highest portion of company specific risk
that can be diversified away in a diversified portfolio like the market portfolio.
So we select your constituents and the market portfolio and the last three columns the total risk systematic
risk and on systematic risk and create a stack the bomb plot.
So let's have a look here a. factually and blew the systematic risk.
And actually the evaded ever it's a systematic risk off the constituents that give us actually the systematic
risk of the market portfolio which is also the total risk of the market portfolio.
So by definition the market portfolio does not contain any answers dramatic risk and in green we can
see here the and systematic risk of the constituents that can be diversified that way if we create a
portfolio instead of investing into these single stocks.
And obviously the highest portion of and systematic risk has the Facebook stock here and now on the
next level can also normalize the systematic risk and create the beta factor and we can create a new
column.
So the beta factor is actually the systematic risk divided by the total risk of the market portfolio
and here we have on the right hand side the beta factor.
And by definition the market portfolio as a beta effect of 1.
And the question is here which company has the highest beta factor.
And the question could also be which the stock is the most cyclical stock and uh that's obviously the
Facebook stock with a beta effect of 1 point to 6.
Next we have to create the security market line and we have to determine the stocks that over performed
and underperformed the market in terms of systematic risk return.
So we created a scatter plot twists on the x axis.
The beta factor and on the y axis.
The return and we actually also label here the points.
Then we also create a pointer for the risk free asset.
And then we create a line from the risk free asset to the market portfolio.
So let's have a look here.
So that's the security market line from the risk free asset to the market portfolio and actually there's
only one stock that is below the security market line and it's American Express.
So American Express throaty and underperformance and actually all of the other stocks over performed
the market in terms of systematic risk and return and finally we can also add another column with the
uh required to cap em return.
So according to the cap I'm model and the D cap M is simply the return of the risk free asset plus the
return off for the market portfolio minus the risk free asset times that paid off the stock and then
you can also calculate an add to the IFR for each constituent which is simply the actual return minus
B cap m return and it's look here.
And the question is here which companies showed the highest and the lowest Alpha and we can either answer
this graphically.
So the alpha of nothing else than the distance between the constituents here and the security market
line and the obviously the highest that far could have uh the Facebook stock.
And as we have seen before.
So the only stock that has a negative Alpha is actually American Express.
But we can also make this more explicit by calculating the alpha and let's have a look here.
So for Facebook we have a positive five five twelve percent because uh the required return according
to the cap and model is 11 percent and the realized return is 22 percent.
So that's a difference of twelve percent around that here actually.
And as we have seen before the American Express stock show to the lowest alpha with minus 4 percent.
So that was context size 16 I hope you had fun and talk to senior also in the next exercise by.